package com.example.slidetabbar;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.content.res.ColorStateList;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import fragments.Frg1;
import fragments.Frg2;
import fragments.Frg3;
import fragments.Frg4;
import fragments.Frg5;
import fragments.Frg6;
import fragments.Frg7;
import fragments.Frg8;
import fragments.ItemVPHomeAdapter;

public class MainActivity extends FragmentActivity implements OnPageChangeListener {
	// ----->复制
	private HorizontalScrollView hs_activity_tabbar;
	private RadioGroup myRadioGroup;
	private List<String> titleList;
	private LinearLayout ll_activity_tabbar_content;
	private float mCurrentCheckedRadioLeft;// 当前被选中的RadioButton距离左侧的距离
	private String channel;

	// <-----复制

	private ViewPager vp;
	private Frg1 frg1 = new Frg1();
	private Frg2 frg2 = new Frg2();
	private Frg3 frg3 = new Frg3();
	private Frg4 frg4 = new Frg4();
	private Frg5 frg5 = new Frg5();
	private Frg6 frg6 = new Frg6();
	private Frg7 frg7 = new Frg7();
	private Frg8 frg8 = new Frg8();
	private Fragment[] fragments = { frg1, frg2, frg3, frg4, frg5, frg6, frg7, frg8 };
	public FragmentManager mFragmentManager = getSupportFragmentManager();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		vp = (ViewPager) findViewById(R.id.view_vp);
		vp.setOnPageChangeListener(this);

		ItemVPHomeAdapter vpAdapter = new ItemVPHomeAdapter(mFragmentManager, fragments);
		vp.setOffscreenPageLimit(2);
		vp.setAdapter(vpAdapter);
		// 导航栏的title集
		titleList = new ArrayList<String>();
		titleList.add("推荐");
		titleList.add("热点");
		titleList.add("北京");
		titleList.add("体育");
		titleList.add("娱乐");
		titleList.add("足球");
		titleList.add("巴萨");
		titleList.add("汽车");

		initGroup();
	}

	private void initGroup() {
		hs_activity_tabbar = (HorizontalScrollView) findViewById(R.id.hs_activity_tabbar);
		ll_activity_tabbar_content = (LinearLayout) findViewById(R.id.ll_activity_tabbar_content);
		// 选项卡布局
		myRadioGroup = new RadioGroup(this);
		myRadioGroup.setLayoutParams(
				new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
		myRadioGroup.setOrientation(LinearLayout.HORIZONTAL);
		ll_activity_tabbar_content.addView(myRadioGroup);
		for (int i = 0; i < titleList.size(); i++) {
			String channel = titleList.get(i);

			RadioButton radio = new RadioButton(this);
			radio.setButtonDrawable(android.R.color.transparent);
			radio.setBackgroundResource(R.drawable.radiobtn_selector);
			ColorStateList csl = getResources().getColorStateList(R.color.radiobtn_text_color);
			radio.setTextColor(csl);
			LinearLayout.LayoutParams l = new LinearLayout.LayoutParams((int) SizeHelper.dp2px(this, 80),
					ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER);
			radio.setLayoutParams(l);
			radio.setTextSize(15);
			radio.setGravity(Gravity.CENTER);
			radio.setText(channel);
			radio.setTag(channel);

			myRadioGroup.addView(radio);

		}

		myRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				int radioButtonId = group.getCheckedRadioButtonId();
				// 根据ID获取RadioButton的实例
				RadioButton rb = (RadioButton) findViewById(radioButtonId);
				channel = (String) rb.getTag();
				// int targetNum=(Integer) rb.getTag();
				mCurrentCheckedRadioLeft = rb.getLeft();// 更新当前按钮距离左边的距离
				int width = (int) SizeHelper.dp2px(MainActivity.this, 140);
				hs_activity_tabbar.smoothScrollTo((int) mCurrentCheckedRadioLeft - width, 0);

				vp.setCurrentItem(titleList.indexOf(channel));

			}
		});
		// 设定默认被选中的选项卡为第一项
		if (!titleList.isEmpty()) {
			myRadioGroup.check(myRadioGroup.getChildAt(0).getId());
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int position) {
		myRadioGroup.check(myRadioGroup.getChildAt(position).getId());
		}

	

}

